package com.example.administrator.xiaoxiang.widget;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.utils.DensityUtil;

/**
 * Created by kk on 2017/1/10.
 */

public class MyCoustomView extends View {


    private Paint paint;
    private int radius;
    private String resultString = "最高可获得的小象金额";
    private String resultString2 = "10,000.00";
    private int rateEnd = 0;

    public MyCoustomView(Context context) {
        super(context);
        init(context);
    }

    public MyCoustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyCoustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    int result;
    Paint paint1, paint2, paint3,paint4;
    Paint ratePaint;
    String[] stringArray = new String[]{"0", "1000", "2000", "3000", "4000", "5000", "6000", "7000", "8000", "9000", "10000"};

    private void init(Context context) {
        paint = new Paint();
        //是否抗锯齿
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        //实心还是空心
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(30);
        //0-255，越小越透明
        paint.setAlpha(60);

        //dp和px之间互转工具类

       // result = DensityUtil.dip2px(300);
        result = DensityUtil.dip2px(context,250);

        //刻度画笔
        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1.setColor(Color.WHITE);
        paint1.setStrokeWidth(6);

        //刻度字画笔
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2.setTextSize(26);
        paint2.setColor(Color.WHITE);


        //正中间字体画笔
        paint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint3.setTextSize(30);
        paint3.setColor(Color.WHITE);

        paint4 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint4.setTextSize(50);
        paint4.setColor(Color.YELLOW);

        ratePaint = new Paint();
        ratePaint.setColor(getResources().getColor(R.color.colorAccent));
        ratePaint.setStrokeWidth(4);
        ratePaint.setStyle(Paint.Style.STROKE);
        //背景渐变动画实现
        ValueAnimator coloranim = ObjectAnimator.ofInt(this, "backgroundColor", 0xFFFF8080, 0xFFFF8080);
        coloranim.setDuration(30 * 180);
        coloranim.setEvaluator(new ArgbEvaluator());
        coloranim.start();

    }

    int px,py;
    int rotateRadius=210/10;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        px=getMeasuredWidth()/2;
        py=getMeasuredHeight()/2;

        //粗圆环
        RectF rectF=new RectF(getLeft()+60,getTop()+60,getRight()-60,getBottom()-60);
        canvas.drawArc(rectF, 165, 210, false, paint);


        //细圆环
        RectF rectFA=new RectF(getLeft()+20,getTop()+20,getRight()-20,getBottom()-20);
        paint.setStrokeWidth(4);
        canvas.drawArc(rectFA, 165, 210, false, paint);

        //动态通知变化的细圆环
        canvas.drawArc(rectFA,165,rateEnd,false,ratePaint);


        //正中间的字体
        if (!resultString.equals(""))
        {
            float textWidth=paint3.measureText(resultString);
            canvas.drawText(resultString,px-textWidth/2,700
                    ,paint3);
        }
        if (!resultString2.equals(""))
        {
            float textWidth=paint4.measureText(resultString2);
            canvas.drawText(resultString2,px-textWidth/2,800,paint4);
        }

        //旋转到正中心。
        canvas.rotate(255,getMeasuredWidth()/2,getMeasuredHeight()/2
        );

        //刻度线起始和终止的两个值
        int lineStartY=getTop()+60-30/2;
        int lineEndY=getTop()+60-30/2+30;
        for (int i=1;i<12;i++)
        {
            canvas.save();
            if (i%2!=0)
            {
                //画明显的刻度
                paint1.setAlpha(160);
                canvas.drawLine(px, lineStartY, px, lineEndY, paint1);
                paint2.setAlpha(180);

            }else
            {
                paint1.setAlpha(100);
                canvas.drawLine(px, lineStartY, px, lineEndY, paint1);
                paint2.setAlpha(120);
            }
            float textLength=paint2.measureText(stringArray[i-1]);
            canvas.drawText(stringArray[i-1],px-textLength/2,lineEndY+30,paint2);
            canvas.restore();
            canvas.rotate(rotateRadius,px,py);
        }
        canvas.restore();
        paint.setStrokeWidth(30);
        //开线程刷新rateEnd的值
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (rateEnd<180)
                {
                    rateEnd++;
                    postInvalidateDelayed(30);
                }
            }
        }).start();

    }

}

